package com.ip.getip.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

import com.ip.getip.dao.GreenRepository;
import com.ip.getip.entity.Green;
import com.ip.getip.entity.MailEntity;
import com.ip.getip.util.MailUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @ Description   :
 * @ Author        :  leixin
 * @ CreateDate    :  2021/12/20-11:11
 * @ Version       :  1.0
 */
@Service
@Slf4j
public class GreenImpl {

    @Autowired
    GreenRepository repository;

    @Autowired
    MailUtils mailUtils;

    @Value("${qq.mail.passwd}")
    private String passwd;
    @Value("${qq.mail.accountNumber}")
    private String accountNumber;
    @Value("${qq.mail.makeACopyFor}")
    private List<String> makeACopyFor;

    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private DateTimeFormatter formatterMM = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    @Scheduled(cron = "0 45 8 ? * MON-FRI ")
    public String clockIn() throws Exception {
        log.info("开始进行上午打卡任务:{}", LocalDateTime.now().format(formatter));
        String url = "http://221.237.162.142:13321/jplat-mobile-oa-client/attendances/add";
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        Green all = repository.findAll().get(0);
        int number = new Random().nextInt(300);
        log.info("延时:{}", number);
        MailEntity mailEntity = new MailEntity();
        mailEntity.setSender(accountNumber);
        mailEntity.setSenderPass(passwd);
        mailEntity.setAddressees(makeACopyFor);
        mailEntity.setEmailSubject("上午打卡");
        mailEntity.setContent("当前延迟补救时间 :" + number+"秒");
        mailUtils.mailSender(mailEntity);
        Thread.sleep(number * 1000);
        if (all.getStartSwitch() == 1) {
            headers.set("Authorization", all.getName());
            MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
            params.add("locale", "四川省成都市双流县和祥一街");
            params.add("coordsType", "bd09ll");
            params.add("coords",
                "{\"latitude\":30.527991,\"longitude\":104.110624,\"accuracy\":40,\"altitude\":5e-324,\"heading\":null,\"speed\":0,\"altitudeAccuracy\":0}");
            params.add("numTime", System.currentTimeMillis() + "");
            params.add("createDay", LocalDateTime.now().format(formatterMM));
            params.add("createTime", "08:45:10");
            params.add("picUrl", null);
            params.add("soleUuid", null);
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                params, headers);
            //执行HTTP请求，将返回的结构使用ResultVO类格式化
            ResponseEntity<String> response = client.postForEntity(url, requestEntity, String.class);
            return response.getBody();
        }
        return "任务未开启";
    }

    @Scheduled(cron = "0 10 18 ? * MON-FRI ")
    public String clockOut() throws Exception {
        log.info("开始进行下午打卡任务:{}", LocalDateTime.now().format(formatter));
        String url = "http://221.237.162.142:13321/jplat-mobile-oa-client/attendances/add";
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        Green all = repository.findAll().get(0);
        int number = new Random().nextInt(300);
        log.info("延时:{}", number);
        MailEntity mailEntity = new MailEntity();
        mailEntity.setSender(accountNumber);
        mailEntity.setSenderPass(passwd);
        mailEntity.setAddressees(makeACopyFor);
        mailEntity.setEmailSubject("下午打卡");
        mailEntity.setContent("当前延迟补救时间 :" + number+"秒");
        mailUtils.mailSender(mailEntity);
        Thread.sleep(number * 1000);
        if (all.getEndSwitch() == 1) {
            headers.set("Authorization", all.getName());
            MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
            params.add("locale", "四川省成都市双流县和祥一街");
            params.add("coordsType", "bd09ll");
            params.add("coords",
                "{\"latitude\":30.527991,\"longitude\":104.110624,\"accuracy\":40,\"altitude\":5e-324,\"heading\":null,\"speed\":0,\"altitudeAccuracy\":0}");
            params.add("numTime", System.currentTimeMillis() + "");
            params.add("createDay", LocalDateTime.now().format(formatterMM));
            params.add("createTime", "18:10:10");
            params.add("picUrl", null);
            params.add("soleUuid", null);
            HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                params, headers);
            //执行HTTP请求，将返回的结构使用ResultVO类格式化
            ResponseEntity<String> response = client.postForEntity(url, requestEntity, String.class);
            return response.getBody();
        }
        return "任务未开启";
    }
}
